﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data.EntityClient;

namespace VLUSM_BusinessLogic.Helpers
{
    public sealed class EntityConnectionCreator
    {
        private static EntityConnectionCreator instance;
        private static object syncRoot = new Object();

        SqlConnectionStringBuilder sqlBuilder;
        EntityConnectionStringBuilder DVHCBuilder;
        EntityConnectionStringBuilder diemBuilder;
        EntityConnectionStringBuilder mainDataBuilder;
        EntityConnectionStringBuilder viewBuilder;

        EntityConnection mainDataConnection;
        EntityConnection donViHanhChinhConnection;
        EntityConnection diemConnection;
        EntityConnection viewConnection;

        public System.Data.EntityClient.EntityConnection ViewConnection
        {
            get { return viewConnection; }
            set { viewConnection = value; }
        }
        public EntityConnection MainDataConnection
        {
            get { return mainDataConnection; }
            set { mainDataConnection = value; }
        }

        public EntityConnection DonViHanhChinhConnection
        {
            get { return donViHanhChinhConnection; }
            set { donViHanhChinhConnection = value; }
        }

        public EntityConnection DiemConnection
        {
            get { return diemConnection; }
            set { diemConnection = value; }
        }

        private EntityConnectionCreator()
        {
            sqlBuilder = new SqlConnectionStringBuilder();
            sqlBuilder.DataSource = @".\SQLEXPRESS";
            sqlBuilder.InitialCatalog = "VLUSM";
            sqlBuilder.IntegratedSecurity = true;
            sqlBuilder.MultipleActiveResultSets = true;
            sqlBuilder.PersistSecurityInfo = true;
            //sqlBuilder.UserID = "sa";
            //sqlBuilder.Password = "123";

            #region Generate Entity Connection
            DVHCBuilder = new EntityConnectionStringBuilder();
            DVHCBuilder.ProviderConnectionString = sqlBuilder.ToString();
            DVHCBuilder.Provider = "System.Data.SqlClient";
            DVHCBuilder.Metadata = String.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "DonViHanhChinh");

            diemBuilder = new EntityConnectionStringBuilder();
            diemBuilder.ProviderConnectionString = sqlBuilder.ToString();
            diemBuilder.Provider = "System.Data.SqlClient";
            diemBuilder.Metadata = String.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "Diem");

            mainDataBuilder = new EntityConnectionStringBuilder();
            mainDataBuilder.ProviderConnectionString = sqlBuilder.ToString();
            mainDataBuilder.Provider = "System.Data.SqlClient";
            mainDataBuilder.Metadata = String.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "MainData");

            viewBuilder = new EntityConnectionStringBuilder();
            viewBuilder.ProviderConnectionString = sqlBuilder.ToString();
            viewBuilder.Provider = "System.Data.SqlClient";
            viewBuilder.Metadata = String.Format("res://*/{0}.csdl|res://*/{0}.ssdl|res://*/{0}.msl", "View");

            donViHanhChinhConnection = new EntityConnection(DVHCBuilder.ToString());
            mainDataConnection = new EntityConnection(mainDataBuilder.ToString());
            diemConnection = new EntityConnection(diemBuilder.ToString());
            viewConnection = new EntityConnection(viewBuilder.ToString());
            #endregion
        }

        public static EntityConnectionCreator Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new EntityConnectionCreator();
                    }
                }
                return instance;
            }
        }
    }
}
